package com.oasys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.oasys.dao.NoticeMapper;
import com.oasys.entity.LoginUser;
import com.oasys.entity.NoticePageVo;
import com.oasys.entity.NoticePo;
import com.oasys.enums.OaResponseCode;
import com.oasys.service.NoticeService;
import com.oasys.service.SysUserService;
import com.oasys.utils.AutoGenerateIds;
import com.oasys.utils.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: TODO
 * @Author: mis_wu
 * @Date: 2022/5/13
 **/
@Service
@Transactional(rollbackFor = Exception.class)
@Slf4j
public class NoticeServiceImpl implements NoticeService {

    @Resource
    private NoticeMapper noticeMapper;

    @Resource
    private SysUserService sysUserService;

    /**
     * 发布公告
     * @param po p
     * @return
     */
    @Override
    public ResponseResult<Boolean> publishNotice(NoticePo po) {
        LoginUser loginUser = sysUserService.getLoginUser();
        if (ObjectUtils.isEmpty(loginUser)){
            return new ResponseResult<>(OaResponseCode.USER_NOT_LOGIN.getCode(),OaResponseCode.USER_NOT_LOGIN.getMsg(),false);
        }
        if (ObjectUtils.isNotEmpty(po)){
            po.setNid(AutoGenerateIds.getGenerateId());
            po.setUserId(loginUser.getUserId());
            po.setNoteDate(new Date());
            //状态 0:正常  1:隐藏
            po.setNoteState(0);
            Boolean notice = noticeMapper.publishNotice(po);
            return new ResponseResult<>(OaResponseCode.SUCCESS.getCode(),OaResponseCode.SUCCESS.getMsg(),notice);
        }
        return new ResponseResult<>(OaResponseCode.FAIL.getCode(),OaResponseCode.FAIL.getMsg(),false);
    }

    /**
     * 查询
     * @param vo
     * @return
     */
    @Override
    public ResponseResult<Map<String,Object>> getNoticeByPage(NoticePageVo vo) {
        try {
            Map<String,Object> map = new HashMap<>(8);
            PageHelper.startPage(vo.getPage(),vo.getSize());
            LoginUser loginUser = sysUserService.getLoginUser();
            if (ObjectUtils.isEmpty(loginUser)){
                map.put("msg","请登录!!!");
                return new ResponseResult<>(OaResponseCode.USER_NOT_LOGIN.getCode(),OaResponseCode.USER_NOT_LOGIN.getMsg(),map);
            }
            vo.setUserId(loginUser.getUserId());
            List<NoticePo> list = noticeMapper.getNoticeByPage(vo);
            for (NoticePo po : list) {
                po.setStrNid(String.valueOf(po.getNid()));
            }
            PageInfo<NoticePo> pageInfo = new PageInfo<>(list);
            map.put("noteList",pageInfo.getList());
            map.put("totals",pageInfo.getTotal());
            return new ResponseResult<>(OaResponseCode.SUCCESS.getCode(),OaResponseCode.SUCCESS.getMsg(),map);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 隐藏
     * @param noteId
     * @return
     */
    @Override
    public Boolean hideNotice(Long noteId) {
        log.info("隐藏:{}",noteId);
        return noticeMapper.hideNotice(noteId);
    }

    /**
     * 取消隐藏
     * @param noteId
     * @return
     */
    @Override
    public Boolean cancelHideNotice(Long noteId) {
        return noticeMapper.cancelHideNotice(noteId);
    }

    /**
     * del
     * @param noteId
     * @return
     */
    @Override
    public Boolean deleteNote(Long noteId) {
        return noticeMapper.deleteNote(noteId);
    }
}
